﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.Threading;

namespace Wilke.Interactive.Drone.PlayGround.UserControls
{
    /// <summary>
    /// Interaction logic for ButtonHeader.xaml
    /// </summary>
    public partial class ButtonHeader : UserControl
    {
        public event EventHandler<SegmentClickedEventArgs> SegmentClicked;
        public bool NeedResize { get; set; }

        public string Title 
        {
            set { textBlock1.Text = value; } 
        }

        public ButtonHeader()
        {
            InitializeComponent();
        }

        private void buttonHome_Click(object sender, RoutedEventArgs e)
        {
            popupPieMenu.PopupAnimation = PopupAnimation.Fade;
            popupPieMenu.IsOpen = true;
            popupPieMenu.PlacementRectangle = new Rect((ActualWidth - popupPieMenu.Width) / 2, 70, 1, 1);
        }

        private void PieMenu_SegmentClicked(object sender, SegmentClickedEventArgs e)
        {
            popupPieMenu.IsOpen = false;

            switch (e.SegmentIndex)
            {
                case 1:
                    //Resize need to be dealt with in the popup closed in order to avoid
                    //awkward positioning of the menu durig resize.
                    NeedResize = true;
                    break;
                default:
                    if (SegmentClicked != null)
                    {
                        SegmentClicked(this, e);
                    }
                    break;
            }
        }

        private Window GetParentWindow()
        {
            DependencyObject parent = this.Parent;

            while (parent != null && !(parent is Window))
            {
                parent = LogicalTreeHelper.GetParent(parent);
            }

            return parent as Window;
        }

        private void popupPieMenu_Closed(object sender, EventArgs e)
        {
            if (NeedResize)
            {
                Window parentWindow = GetParentWindow();

                if (parentWindow != null)
                {
                    if (parentWindow.WindowState == WindowState.Maximized)
                    {
                        parentWindow.WindowState = WindowState.Normal;
                        parentWindow.WindowStyle = WindowStyle.ThreeDBorderWindow;
                    }
                    else
                    {
                        parentWindow.WindowState = WindowState.Normal;
                        parentWindow.WindowStyle = WindowStyle.None;
                        parentWindow.Topmost = true;
                        parentWindow.WindowState = WindowState.Maximized;
                    }
                }

                NeedResize = false;
            }
        }

        public void SelectSegment(int index, bool selected)
        {
            pieMenu.SelectSegment(index, selected);
        }

        public void SetConnectState(bool connected)
        {
            Style style = this.FindResource(connected ? "GlassMenuButtonConnected" : "GlassMenuButtonDeconnected") as Style;
            buttonHome.Style = style;
        }
    }
}
